<template>
  <div class="inner">
    <div class="left">
      <div class="container-tree">
        <el-tree @node-click="handleNodeClick" ref="orgTree" :data="treeList" :current-node-key="defaultExpandedId"
          node-key="id" :props="defaultProps" highlight-current icon-class=" "
          :default-expanded-keys="[defaultExpandedId]" accordion>
          <span class="custom-tree-node" slot-scope="{ node, data }">
            <span v-if="node.level == 1" class="father">
              <!-- <i class="el-icon-school"></i> -->
              <svg-icon icon-class="warehouse" :color="data.id === currentNodeKey ? '#ffffff':'#000000'"></svg-icon>
              <span class="text">
                <span style="margin: 0 5px">{{ data.code }}</span>
                {{ data.name }}</span>
              <el-dropdown trigger="hover" v-if="$showFn(['editwarehouse', 'delwarehouse'])">
                <span class="el-dropdown-link">
                  <i class="el-icon-more" :style="{'color':data.id === currentNodeKey ?'#fff':'#000'}"/>
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item icon="el-icon-edit" @click.native="toWarehouseEdit(data.id)"
                    v-has="'editwarehouse'">修改</el-dropdown-item>
                  <el-dropdown-item icon="el-icon-delete" @click.native="
                    $delFn('仓库', delWarehouseById, data.id)
                    " v-has="'delwarehouse'">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </span>
            <span v-else-if="node.level == 2" class="son">{{ data.name }} ({{ data.positionCount }})</span>
            {{ data.label }}
          </span>
        </el-tree>
      </div>
      <el-button class="add-btn" size="default" @click="$router.push({ name: 'warehouseAdd' })"
        v-has="'addwarehouse'">
        添加仓库 <i class="el-icon-plus"></i> 
      </el-button>
    </div>
    <div class="right">
      <!-- 父节点展示信息 -->
      <FatherTreeNode v-if="type === 0" :warehouseId="parentNodeKey" @getWarehouseTreeList="getWarehouseTreeList">
      </FatherTreeNode>
      <!-- 子节点展示信息 -->
      <ChildrenTreeNode v-else :childrenId="childrenNodeKey" :warehouseId="parentNodeKey"
        @getWarehouseTreeList="getWarehouseTreeList"></ChildrenTreeNode>
    </div>
  </div>
</template>

<script>
import FatherTreeNode from "./components/FatherTreeNode";
import ChildrenTreeNode from "./components/ChildrenTreeNode";
import { reqWarehouseTreeList, reqDelWarehouseById } from "@/api/warehouse";
export default {
  name: "WarehouseManager",
  components: { FatherTreeNode, ChildrenTreeNode },
  data() {
    return {
      isFirst: true,
      // 判断父节点还是子节点
      type: 0,
      // 树形结构
      treeList: [],
      // 树形结构的对应字段名
      defaultProps: {
        children: "warehouseRegionList",
        label: "name",
      },
      // 当前展开的id
      currentNodeKey: "",
      // 默认高亮和展开的节点
      defaultExpandedId: "",
      // 父id
      parentNodeKey: "",
      // 子id
      childrenNodeKey: "",
    };
  },
  created() {
    this.getWarehouseTreeList(); // 获取左侧树形列表
  },
  methods: {
    // 树被点击发生的事
    handleNodeClick(data) {
      this.type = data.positionCount === undefined ? 0 : 1;
      // 防止树无法合上
      this.defaultExpandedId = data.id;
      this.parentNodeKey =
        data.positionCount === undefined ? data.id : this.parentNodeKey;
      this.currentNodeKey = data.id;
      if (this.parentNodeKey != this.currentNodeKey) {
        this.childrenNodeKey = this.currentNodeKey;
      }
    },
    // 获取树和首次加载详情
    async getWarehouseTreeList() {
      let res = await reqWarehouseTreeList();
      if (res.code === 200) {
        this.treeList = res.data;
        if (this.treeList.length > 0) {
          // 保存当前节点
          this.currentNodeKey = this.currentNodeKey
            ? this.currentNodeKey
            : this.treeList[0].id;
          this.defaultExpandedId = this.currentNodeKey;
          this.$nextTick(() => {
            this.$refs["orgTree"].setCurrentKey(this.currentNodeKey);
          });
          // 保存父节点
          if (this.isFirst) {
            this.parentNodeKey = this.treeList[0].id;
            this.isFirst = false;
          }
        }
      }
    },
    // 修改仓库
    toWarehouseEdit(id) {
      this.$router.push({ path: "warehouseEdit", query: { id } });
    },
    // 删除仓库
    async delWarehouseById(id) {
      let res = await reqDelWarehouseById({ id });
      if (res.code === 200) {
        this.$message.success("删除成功");
        this.getWarehouseTreeList();
      } else {
        // this.$message.error(res.message);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.inner {
  display: flex;
  height: 100%;

  .left {
    width: 280px;
    padding: 16px;
    height: 100%;
    border-right: 1px solid #e7e7e7;
    display: flex;
    flex-direction: column;
    justify-content: space-between;

    .container-tree {
      border-radius: 8px;
      // width: 240px;
      width: 100%;
      height: 750px;
      overflow: auto;
      overflow-x: hidden;
      position: relative;
      padding: 10px;

      .custom-tree-node {
        width: 100%;

        .father {
          width: 100%;
          display: flex;
          justify-content: center;
          align-items: center;
          // color: #1a1a1a;

          .el-dropdown-link {
            font-size: 20px;
            margin-right: 10px;
          }

          .text {
            flex: 1;
          }
        }
      }

      ::v-deep .el-tree-node__content {
        padding-top: 25px !important;
        padding-bottom: 25px !important;

        .el-tree-node__label {
          font-size: 16px !important;
        }
      }

      ::v-deep .el-tree--highlight-current {
        .el-tree-node.is-current>.el-tree-node__content {
          background-color: #0052D9 !important;
          border-radius: 5px !important;
          color: #fff !important;
          span{
            color: #fff !important;
          }
        }
      }
    }

    .add-btn {
      background-color: #0052D9;
      color: #fff
    }
  }
  .right{
    flex: 1;
    width: 300px;
    height: 100%;
  }
}
</style>
